import com.sun.glass.ui.Screen;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Arrays;

/**
 * Created by ospip on 2017/10/30.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class Calculator extends JFrame {
    private BorderLayout wrapper;
    private GridLayout gridMain;
    private JPanel panelMain;
    private JButton[] buttons;
    private JTextField result;
    private double x;
    private double lHand, rHand;
    private String operator = "";

    private DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
    private String[] stringButton = {
            "%", "√", "^2", "1/x",
            "CE", "C", "del", "÷",
            "7", "8", "9", "×",
            "4", "5", "6", "-",
            "1", "2", "3", "+",
            "±", "0", ".", "="
    };

    public Calculator() {
        super("计算器");
        try {
            UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        wrapper = new BorderLayout(5, 5);
        gridMain = new GridLayout(6, 4, 5, 5);
        panelMain = new JPanel();
        panelMain.setLayout(gridMain);
        result = new JTextField();
        buttons = new JButton[24];

        this.setSize(255, 255);
        this.setBounds(5, 5, 245, 245);
        this.setResizable(true);
        this.setLocation(
                ((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 255) / 2,
                ((int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() - 255) / 2
        );
        this.setLayout(wrapper);
        this.add(result, BorderLayout.NORTH);
        this.add(panelMain, BorderLayout.CENTER);

        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(stringButton[i]);
            buttons[i].addActionListener((e) -> {
                String actionText = ((JButton)e.getSource()).getText();
                if (actionText.equals(".") || actionText.length() == 1 && '0' <= actionText.charAt(0) && actionText.charAt(0) <= '9') {
                    result.setText(result.getText() + actionText);
                }
                else {
                    switch(actionText) {
                        case "del":
                            if (result.getText().length() > 0) result.setText(result.getText().substring(0, result.getText().length() - 1));
                            break;
                        case "±":
                            if (result.getText().charAt(0) == '-') result.setText(result.getText().substring(1));
                            else result.setText("-" + result.getText());
                            break;
                        case "^2":
                            try {
                                x = Double.parseDouble(result.getText());
                                result.setText(decimalFormat.format(x * x));
                            }
                            catch (NumberFormatException ex) {
                                result.setText(ex.getMessage());
                            }
                            break;
                        case "1/x":
                            try {
                                x = Double.parseDouble(result.getText());
                                result.setText(decimalFormat.format(1 / x));
                            }
                            catch (NumberFormatException ex) {
                                result.setText(ex.getMessage());
                            }
                            catch (ArithmeticException ex) {
                                result.setText("不能对0取倒数");
                            }
                            break;
                        case "√":
                            try {
                                x = Double.parseDouble(result.getText());
                                if (x < 0) throw new ArithmeticException("");
                                result.setText(decimalFormat.format(Math.sqrt(x)));
                            }
                            catch (NumberFormatException ex) {
                                result.setText(ex.getMessage());
                            }
                            catch (ArithmeticException ex) {
                                result.setText("不能对负数开平方");
                            }
                            break;
                        case "%":
                            try {
                                x = Double.parseDouble(result.getText());
                                result.setText(decimalFormat.format(x / 100));
                            }
                            catch (Exception ex) {
                                result.setText(ex.getMessage());
                            }
                            break;
                        case "+":

                            break;
                        case "-":

                            break;
                        case "×":

                            break;
                        case "÷":

                            break;
                    }
                }
            });
            panelMain.add(buttons[i]);
        }

        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new Calculator();
    }
}
